package com.example.Service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.Service.InteractionService;
import com.example.dao.InteractionMapper;
import com.example.pojo.Interaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.Base64;
import java.util.List;

@Service
public class InteractionServiceImpl extends ServiceImpl<InteractionMapper, Interaction> implements InteractionService  {

    @Autowired
    private InteractionMapper interactionMapper;

    public List<Interaction> selectAllPeople() {
        return interactionMapper.selectAllPeople();
    }

    public List<Interaction> selectMessagesUserConversations(String sender, String recipient) {
        List<Interaction> interactionList = interactionMapper.selectMessagesUserConversations(sender, recipient);
//        for (Interaction interaction : interactionList) {
//            Blob blob = interaction.getFile();
//            if (blob != null) {
//                try {
//                    String base64String = convertBlobToBase64String(blob);
//                    interaction.setFiles(base64String);
//                } catch (IOException e) {
//                    e.printStackTrace();
//                } catch (SQLException throwables) {
//                    throwables.printStackTrace();
//                }
//            }
//        }
        return interactionList;
    }
    private String convertBlobToBase64String(Blob blob) throws IOException, SQLException {
        byte[] bytes = blob.getBytes(1, (int) blob.length());
        return Base64.getEncoder().encodeToString(bytes);
    }


    public void insertSendMessageByNameAndRecipient(Interaction interaction) {
       interactionMapper.insertSendMessageByNameAndRecipient(interaction);
    }

}
